﻿using Lamex.Base;
using Lamex.Interface;
using Lamex.Library;
using Lamex.mDashboard.BLL;
using Lamex.mDashboard.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mDashboard.UI
{
    public partial class FrmPPC_ReMainderMat : frmBaseChild
    {
        public FrmPPC_ReMainderMat()
        {
            InitializeComponent();
        }

        private void FrmPPC_ReMainderMat_Load(object sender, EventArgs e)
        {
            try { this.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理
            DateEditFormat();
        }
        public void DateEditFormat()
        {
            this.dtStartTime.Properties.DisplayFormat.FormatString = "yyyy-MM-dd";
            this.dtStartTime.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtStartTime.Properties.EditFormat.FormatString = "yyyy-MM-dd";
            this.dtStartTime.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtStartTime.Properties.Mask.EditMask = "yyyy-MM-dd";

            this.dtEndTime.Properties.DisplayFormat.FormatString = "yyyy-MM-dd";
            this.dtEndTime.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtEndTime.Properties.EditFormat.FormatString = "yyyy-MM-dd";
            this.dtEndTime.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            this.dtEndTime.Properties.Mask.EditMask = "yyyy-MM-dd";

            DateTime dt = DateTime.Now;
            //dtStartTime.Text = dt.AddMonths(-dt.Month + 1).AddDays(-dt.Day + 1).ToString("yyyy-MM-dd"); //本年第一天
            dtStartTime.Text = dt.AddDays(-7).ToString("yyyy-MM-dd");
            dtEndTime.Text = dt.ToString("yyyy-MM-dd");//当前日期
        }
        public override void DoView(IButtonInfo sender)
        {
            if (string.IsNullOrEmpty(dtStartTime.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请选择开始时间", "提示", MessageBoxButtons.OK);
                return;
            }
            if (string.IsNullOrEmpty(dtEndTime.Text))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请选择结束时间", "提示", MessageBoxButtons.OK);
                return;
            }
            gridView1.Columns.Clear();
            DataTable dt = GetData();
            gridControl1.DataSource = dt;
            gridView1.BestFitColumns();
            this.gridView1.OptionsView.ColumnAutoWidth = false;
            Save_Click_Ratio();
            base.DoView(sender);
        }
        public DataTable GetData()
        {
            DataTable dt = new DataTable();
            string sql = "";
            //机器选择
            string machine = string.IsNullOrEmpty(cbType.Text) ? " and MACHINE_NO like '%%' " : " and MACHINE_NO='" + cbType.Text + "' ";
            //时间选择
            string date_time = "and DATE_TS>='" + dtStartTime.Text + " 00:00:00'  and DATE_TS<='" + Convert.ToDateTime(dtEndTime.Text).AddDays(1) + "'";
            //按具体时间查询
            if (string.IsNullOrEmpty(cbSelect.Text.Trim()))
            {
//                sql = @"SELECT 
//       [OPTIMIZENAME] as 图号
//      ,[MACHINE_NO] as 机台
//      ,CONVERT(varchar(10),[DATE_TS],23) 时间
//      ,[LENGHT] 长
//      ,[WIDTH] 宽
//      ,[THICK] 厚
//      ,sum([REMAINDER_QTY])  数量
//      ,sum([LENGHT]*[WIDTH]*[REMAINDER_QTY]/1000000) 面积
//  FROM [Edgeband_Program].[dbo].[HPO_REMAINDER_MAT] m
//  where 1=1 " + machine + "" + date_time + @" 
//  group by [OPTIMIZENAME],[MACHINE_NO],CONVERT(varchar(10),[DATE_TS],23),
//  [LENGHT],[WIDTH],[THICK]";
                sql = @"select DISTINCT 
 A.[OPTIMIZENAME] as 图号
,A.[MACHINE_NO] as 机台
,A.DATE_TS as 时间
,A.[LENGHT] as 长
,A.[WIDTH] as 宽
,A.[THICK] as 厚
,A.[REMAINDER_QTY] as 数量
,A.Areas as 面积
,b.COMPONENT_NAME as 颜色
from (
SELECT 
       [OPTIMIZENAME]
      ,[MACHINE_NO]
      ,CONVERT(varchar(10),[DATE_TS],23) [DATE_TS]
      ,[LENGHT]
      ,[WIDTH]
      ,[THICK]
   ,sum([REMAINDER_QTY]) [REMAINDER_QTY]
   ,sum([LENGHT]*[WIDTH]*[REMAINDER_QTY]/1000000) Areas
  FROM [Edgeband_Program].[dbo].[HPO_REMAINDER_MAT] m
  where 1=1 " + machine + "" + date_time + @" 
  group by [OPTIMIZENAME],[MACHINE_NO],CONVERT(varchar(10),[DATE_TS],23),
  [LENGHT],[WIDTH],[THICK]
  ) A 
 LEFT JOIN  [Edgeband_Program].[dbo].[HPO_TABLE_BOARD] b on A. [OPTIMIZENAME]=b.PLANNAME
  order by A.[OPTIMIZENAME] ";
            }
            //按筛选条件查询
            else
            {
                switch (cbSelect.Text.Trim())
                {
                    case "年":
                        sql = @"select CAST(YEAR(A.DATE_TS) as varchar(4)) 年份,A.THICK as 厚度,SUM(A.Areas) as 面积 from (
SELECT 
       [OPTIMIZENAME]
      ,[MACHINE_NO]
      ,CONVERT(varchar(10),[DATE_TS],23) [DATE_TS]
      ,[LENGHT]
      ,[WIDTH]
      ,[THICK]
      ,sum([REMAINDER_QTY]) [REMAINDER_QTY]
      ,sum([LENGHT]*[WIDTH]*[REMAINDER_QTY]/1000000) Areas
  FROM [Edgeband_Program].[dbo].[HPO_REMAINDER_MAT] m
  where 1=1  --and MACHINE_NO like '%%' and DATE_TS>='2019-11-19 00:00:00'  and DATE_TS<='2019/11/27 0:00:00'  
  group by [OPTIMIZENAME],[MACHINE_NO],CONVERT(varchar(10),[DATE_TS],23),[LENGHT],[WIDTH],[THICK]) A
  GROUP BY CAST(YEAR(A.DATE_TS) as varchar(4)),A.THICK
  order by CAST(YEAR(A.DATE_TS) as varchar(4))";
                        break;
                    case "月":
                        sql = @"select CAST(YEAR(A.DATE_TS) as varchar(4))+'-' +CAST(MONTH(A.DATE_TS) as varchar(2)) AS 月份,A.THICK as 厚度,SUM(A.Areas) as 面积 from (
SELECT 
       [OPTIMIZENAME]
      ,[MACHINE_NO]
      ,CONVERT(varchar(10),[DATE_TS],23) [DATE_TS]
      ,[LENGHT]
      ,[WIDTH]
      ,[THICK]
      ,sum([REMAINDER_QTY]) [REMAINDER_QTY]
      ,sum([LENGHT]*[WIDTH]*[REMAINDER_QTY]/1000000) Areas
  FROM [Edgeband_Program].[dbo].[HPO_REMAINDER_MAT] m
  where 1=1  --and MACHINE_NO like '%%' and DATE_TS>='2019-11-19 00:00:00'  and DATE_TS<='2019/11/27 0:00:00'  
  group by [OPTIMIZENAME],[MACHINE_NO],CONVERT(varchar(10),[DATE_TS],23),[LENGHT],[WIDTH],[THICK]) A
  GROUP BY CAST(YEAR(A.DATE_TS) as varchar(4)) +'-'+CAST(MONTH(A.DATE_TS) as varchar(2)),A.THICK
  order by CAST(YEAR(A.DATE_TS) as varchar(4)) +'-'+CAST(MONTH(A.DATE_TS) as varchar(2))";
                        break;
                }
               
            }

            dt = BaseBuildSql.GetDataTableBySql(sql);
            return dt;
        }
        //导出
        public override void DoExport(IButtonInfo sender)
        {
            try
            {
                SaveFileDialog dlg = new SaveFileDialog();
                //dlg.Filter = "PDF file(*.pdf)|*.pdf";
                dlg.Filter = "Excel文件(*.xls)|*.xls";
                DialogResult dialogResult = dlg.ShowDialog(this);
                if (dialogResult == DialogResult.OK)
                {
                    gridView1.ExportToXls(dlg.FileName);
                    DevExpress.XtraEditors.XtraMessageBox.Show("导出成功", "提示", MessageBoxButtons.OK);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void Save_Click_Ratio()
        {
            Sys_P_ReportInfo model = new Sys_P_ReportInfo();
            model.MODULE = "PPC";//模块名
            model.REPORT_NAME = "余料查询";//报表名称
            model.IP = System.Environment.UserName;//当前用户名
            model.CREATE_TS = DateTime.Now;//创建时间
            model.USERNAME = Lamex.Common.Loginer.CurrentUser.Account;//修改人
            if (BaseFactory<Sys_P_Report>.Instance.Insert(model))
            {

            }
        }
    }
}
